﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;


namespace commonV2.forms
{
    public partial class baseImageProcess : forms.baseForm 
    {
        public bool returnValue = false;
        private string tempFileName = null;
        protected void RemoveTempFile()
        {
            if (tempFileName != null)
            {
                try
                {
                    fileFuncs.FileRemove(tempFileName);
                }
                catch (Exception er)
                {
                    this.ErrorHandler(this,er);
                }
            }
            tempFileName = null;
        }
        protected string GetTempFile(string ext)
        {
            if (tempFileName == null) tempFileName = fileFuncs.GetTempFileName(ext);
            return tempFileName;
        }

        public string myResultFileName = null;
        //private string imageTempFileName =  null;
        public baseImageProcess()
        {
            InitializeComponent();
            emImage.Image = null;
            imageHintLbl.BringToFront();
            ResizeForm();

            openFileDialog.Filter = SysLibs.MakeFileDialogFilter_Image();
            saveFileDialog.Filter = SysLibs.MakeFileDialogFilter_Image(); 
        }
        public Image myImage
        {
            get { return emImage.Image;}
            set 
            { 
                emImage.Image = value;
            }
        }
        protected virtual bool SaveImage()
        {
            this.ShowMessage("");
            if (emImage.Image == null) return false;
            if (saveFileDialog.ShowDialog() != DialogResult.OK)   return false;
            emImage.Image.Save(saveFileDialog.FileName);		// save to file
            this.ShowMessage(String.Format(language.GetString("dataSavedTo"),saveFileDialog.FileName));
            this.myResultFileName = saveFileDialog.FileName;
            return true;
        }
        protected virtual void ResizeForm()
        {
            emImage.Location = new Point(0, buttonPnl.Location.Y + buttonPnl.Height);
            if (this.Width < buttonPnl.Width+2) this.Width = buttonPnl.Width+2;
            if (this.Height < buttonPnl.Width) this.Height = buttonPnl.Width;
            emImage.Size = new Size(this.Width - 2 * SystemInformation.BorderSize.Height - 6,
                                    this.Height - buttonPnl.Height - 2 * SystemInformation.CaptionHeight - 2 * SystemInformation.BorderSize.Width - 6);

            buttonPnl.Location = new Point((this.Width - buttonPnl.Width) / 2, 0);
            this.ShowMessage("","[" + this.Width.ToString() + " x " + this.Height.ToString()+"]");
        }
        protected virtual void GetImage() { return; }
        protected virtual void DisposeImage()
        {
            Image oldImg = emImage.Image;
            // empty picture box
            emImage.Image = null;
            // dispose old image (free memory, unlock file)
            if (oldImg != null) oldImg.Dispose();
        }
        protected virtual void DoClose()
        {
            this.Close();
            returnValue = false;
        }
        protected virtual bool DoSelect()
        {
            this.Close();
            returnValue = true;
            return true;
        }

        public override void SetLanguage()
        {
            base.SetLanguage();
            this.Text = language.GetString("getImage");
            this.getImageBtn.Text = language.GetString("getImage");
            this.imageHintLbl.Text = language.GetString("getImageHint");
            this.openBtn.Text = language.GetString("open");
            this.saveBtn.Text = language.GetString("save");
            this.selectBtn.Text = language.GetString("select");
            this.closeBtn.Text = language.GetString("close");
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            try
            {
                DoClose();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                if (openFileDialog.ShowDialog() != DialogResult.OK) return;
                emImage.Image = images.LoadImage(openFileDialog.FileName);
                this.myResultFileName = openFileDialog.FileName;
                imageHintLbl.SendToBack();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }
        
        private void saveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                SaveImage();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }

        private void getImage_Resize(object sender, EventArgs e)
        {
            try
            {
                ResizeForm();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }

        private void getImage_Load(object sender, EventArgs e)
        {
            if (this.myImage==null) imageHintLbl.BringToFront();
            else imageHintLbl.SendToBack();
        }

        private void getImageBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                GetImage();
                imageHintLbl.SendToBack();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }

        private void selectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                DoSelect();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er); 
            }
        }
    }
}
